#include "queue.h"

Queue *CreateQueue(uint16_t size)
{
    Queue *q = (Queue *)malloc(sizeof(Queue));
    if (!q)
    {
        printf("Queue: no space\n");
        return NULL;
    }
    q->front = NULL;
    q->rear = NULL;
    q->size_max = size;
    q->size_current = 0;
    return q;
}

void AddQ(Queue *q, Object item)
{
    if (q->size_current >= q->size_max)
        DeleteQ(q);

    QNode *qNode = (QNode *)malloc(sizeof(QNode));
    if (!qNode)
    {
        printf("Queue: no space\n");
        return;
    }
    q->size_current++;
    qNode->data = item;
    qNode->next = NULL;
    if (q->front == NULL)
    {
        q->front = qNode;
    }
    if (q->rear == NULL)
    {
        q->rear = qNode;
    }
    else
    {
        q->rear->next = qNode;
        q->rear = qNode;
    }
}

int IsEmptyQ(Queue *q)
{
    return (q->front == NULL);
}

int DeleteQ(Queue *q)
{
    if (IsEmptyQ(q))
    {
        printf("Queue: empty\n");
        return -1;
    }
    QNode *temp = q->front;
    //Object item;
    if (q->front == q->rear)
    { //若队列只有一个元素
        q->front = NULL;
        q->rear = NULL;
    }
    else
    {
        q->front = q->front->next;
    }
    //item = temp->data;
    free(temp);
    return 0;
}

void PrintQueue(Queue *q)
{
    if (IsEmptyQ(q))
    {
        printf("Queue: empty\n");
        return;
    }
    printf("print data in queue：\n");
    QNode *qNode = q->front;
    while (qNode != NULL)
    {
        printf("%f, %f", qNode->data.t, qNode->data.h);
        qNode = qNode->next;
    }
    printf("\n");
}
